package cn.lyd.system.controller;

import cn.lyd.base.until.AjaxResult;
import cn.lyd.base.until.PageList;
import cn.lyd.system.domain.Dictionarydetail;
import cn.lyd.system.query.DictionarydetailQuery;
import cn.lyd.system.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典明细的API",description="数据字典明细相关的CRUD功能")
@RestController //@controller +@ResponseBody 只返回json数据，不经过视图解析器，否则不能用
@RequestMapping("/dictionarydetail")
public class DictionarydetailController {
    @Autowired
    private IDictionarydetailService dictionarydetailService;

    /*restful接口*/

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部" )

    public List<Dictionarydetail> findAll(){
        return dictionarydetailService.findAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Dictionarydetail findById(@PathVariable("id") Long id){
        return dictionarydetailService.findById(id);
    }


    /**
     * 通过type_id查询多个
     * @param id
     * @return
     */
    @GetMapping("/findByTypesId/{id}")
    @ApiOperation(value = "通过ID查询" )
    public List<Dictionarydetail> findByTypesId(@PathVariable("id") Long id){
        return dictionarydetailService.findByTypesId(id);
    }


    /**
     * 添加或修改
     * @param dictionarydetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改" )

    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
        try {
            //判断有无id--判断是添加还是修改
            if (dictionarydetail.getId()==null){
                dictionarydetailService.add(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
            //操作成功调用AjaxResult的无参构造
            return new AjaxResult();
        } catch (Exception e) {//操作失败
            e.printStackTrace();
            //操作失败调用AjaxResult的有参构造
            return new AjaxResult(false,"操作失败");
        }

    }


    /**
     * 分页查询
     * @param dictionarydetailQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )

    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery dictionarydetailQuery){
        return   dictionarydetailService.queryByPage(dictionarydetailQuery);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )

    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            dictionarydetailService.delete(id);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除对象" )
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            //如果删除成功，调用AjaxResult的无参构造
            dictionarydetailService.batchDelete(ids);
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }



}
